Skip to content

Ensure parent directory exists before writing a file#2823

Closed
e-nomem wants to merge 1 commit intoPyO3:mainfrom
e-nomem:file-write-create-parent
Closed

Ensure parent directory exists before writing a file#2823
e-nomem wants to merge 1 commit intoPyO3:mainfrom
e-nomem:file-write-create-parent

Conversation

@e-nomem
Copy link
Copy Markdown
Contributor

@e-nomem e-nomem commented Nov 4, 2025

I ran into a bug when I moved my LICENSE file from the root of the repository to LICENSES/MIT.txt and updated the license-file directive in my pyproject.toml to match. uv build --sdist . and uv build --wheel . both had no issues, but running uv lock -U reported an error.

I tracked the issue down to the fact that both WheelWriter and SdistWriter don't care about adding directories and just stub out their implementation of add_directory(), but PathWriter does, and it needs the parent directory to exist before the file is written.

In this PR I ensured that all non-test code that calls add_bytes_with_permissions() calls add_directory() first. I considered giving add_bytes_with_permissions() a default impl that calls add_directory() and adding a layer of indirection to the trait but decided against it.

Alternately, given that 2 of the 3 main implementations of ModuleWriter don't care about directories, maybe add_directory() shouldn't be part of the trait and should be an internal implementation detail for the PathWriter implementation of add_bytes_with_permissions()? (See #2824 for a take at this)

Comment thread tests/common/metadata.rs
assert_snapshot!(writer.directories.iter().sorted().join("\n"), @r"
hello_world-0.1.0.dist-info
hello_world-0.1.0.dist-info/licenses
hello_world-0.1.0.dist-info/licenses/licenses
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra level of licenses nesting being missing from the previous snapshot output is the exact issue I had

@e-nomem e-nomem force-pushed the file-write-create-parent branch from 6839676 to f597688 Compare November 4, 2025 07:17
@e-nomem e-nomem force-pushed the file-write-create-parent branch from f597688 to e792af1 Compare November 4, 2025 09:07
messense pushed a commit that referenced this pull request Nov 6, 2025
In #2823 I considered the idea of
removing `add_directory()` entirely from the `ModuleWriter` trait since
it seemed error-prone to require the programmer to ensure it was called
everywhere. I decided to make a PR for that as well to see what it would
look like.
@messense messense closed this Nov 6, 2025
@e-nomem e-nomem deleted the file-write-create-parent branch November 6, 2025 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants